<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('员工管理列表')"/>
    <style>
        .layui-card-body .layui-form {
            margin: auto;
        }
        .layui-form-item {
            margin-bottom: 0px;
        }

    </style>
</head>
<body oncontextmenu="return test()">
<div class="layuimini-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form id="formId" class=" layui-form layui-form-pane">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">部门</label>
                        <div class="layui-input-inline">
                            <ul data-id="0" id="selectDept" name="deptId"></ul>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="username" placeholder="请输入名称"
                                   class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">工号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="userSn" placeholder="请输入工号"
                                   class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-md" lay-submit lay-filter="user-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-md layui-btn-primary" id="formreset">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
            <table id="user-table" lay-filter="user-table"></table>
            <script type="text/html" id="user-toolbar">
                <button sec:authorize="hasPermission('/sys/user/add','sys:user:add')"
                        class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                    <i class="layui-icon layui-icon-add-1"></i>
                    新增
                </button>
                <button sec:authorize="hasPermission('/sys/user/import','sys:user:import')"
                        class="layui-btn layui-btn-primary layui-btn-md" lay-event="import">
                    <i class="layui-icon layui-icon-upload"></i>
                    导入
                </button>
                <button sec:authorize="hasPermission('/sys/user/export','sys:dept:export')"
                        class="layui-btn layui-btn-primary layui-btn-md" lay-event="export">
                    <i class="layui-icon layui-icon-download-circle"></i>
                    导出
                </button>
                <button sec:authorize="hasPermission('/sys/user/remove','sys:user:del')"
                        class="layui-btn layui-btn-danger layui-btn-md" lay-event="batchRemove">
                    <i class="layui-icon layui-icon-delete"></i>
                    删除
                </button>
            </script>
            <script type="text/html" id="user-bar">
                <button sec:authorize="hasPermission('/sys/user/edit','sys:user:edit')"
                        class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>
                </button>
                <button sec:authorize="hasPermission('/sys/user/remove','sys:user:del')"
                        class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i
                        class="layui-icon layui-icon-delete"></i>
                </button>
            </script>

        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>

<script id="user-enable" type="text/html">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="user-enable" {{ d.enable== true ? 'checked' : '' }} />
</script>

<script id="user-sex" type="text/html">
    {{#if (d.sex == 0) { }}
    <span>男</span>
    {{# }else if(d.sex == 1){ }}
    <span>女</span>
    {{# } }}
</script>
<script>
    var _formData;
    let table;
    layui.use(['table', 'form', 'jquery', 'dtree'], function () {
        table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;

        let prefix = pName + "/system/user/";
        deptUtil.initDeptSelectTree("selectDept", "deptId","deptName", "0")

        let cols = [
            [
                {type: 'checkbox'},
                        {
                            field: 'username',
                            title: '名称'
                        },
                        {
                            field: 'userSn',
                            title: '工号'
                        },
                        {
                            field: 'deptName',
                            title: '机构名称'
                        },
                {title: '性别', field: 'sex', align: 'center', templet: '#user-sex', width: 60},
                {title: '电话', field: 'phone', align: 'center'},
                {title: '启用', field: 'enable', align: 'center', templet: '#user-enable', width: 120},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#user-table',
            url: prefix + 'data',
            page: true,
            escape: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            ,height: 'full-100'
        });
        table.on('checkbox(user-table)', function(obj){

        });
        table.on('row(user-table)', function (obj) {

        });
        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'import') {
                window.import();
            } else if (obj.event === 'export') {
                window.exportUser();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(user-query)', function (data) {
            _formData = data.field;
            table.reload('user-table', {where: data.field})
            return false;
        });

        form.on('switch(user-enable)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = 1;
            } else {
                operate = 0;
            }
            let loading = layer.load();
            $.ajax({
                url:  prefix + 'enable',
                data: JSON.stringify({id: this.value, enable: operate}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg);
                    } else {
                        layer.alert(result.msg);
                        table.reload("user-table");
                    }
                }
            })
        });

        window.add = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '新增员工管理',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: prefix + 'add'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }
        window.import = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '导入员工管理',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: pName + '/system/common/importMain?dUrl=' +  encodeURIComponent(pName + '/system/file/download/userImport') + '&uUrl=' + encodeURIComponent(pName + '/system/user/upload')
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }
        window.exportUser = function () {
            console.log(JSON.stringify(_formData))
            let dUrl = prefix + "export";
            let formHtml = "";
            for(var k in _formData){
                formHtml += '<input name="'+k+'" value="'+_formData[k]+'" />';
            }
            $('<form id="dataForm" action="'+dUrl+'" method="post">' +
                formHtml +
                '</form>').appendTo('body').submit().remove();
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改员工管理',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该员工管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些员工管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('user-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('user-table', {where: param});
        }

        $('#formreset').click(function(){
            $('#formId').find('input').val("")
        })
    })

    function test(){
        var el=event.srcElement;
        if(el.tagName=="A")
            alert("连接到 :"+el.href);
        else {
            alert("不准用鼠标右键:)");
            window.open(location.href)
        }
        return false;
    }
</script>
</body>
</html>